<template>
	<view class="wrap">
		<view class="comment">
			<view class="top">
				<view class="left">
					<view class="heart-photo"><image :src="comment.headUrl" mode=""></image></view>
					<view class="user-info">
						<view class="name">{{ comment.realName }}</view>
						<view class="date">{{  $u.timeFrom(comment.updateTime, 'yyyy-mm-dd') }}</view>
					</view>
				</view>
			</view>
			<view class="content">{{ comment.content }}</view>
		</view>
		<view class="all-reply">
			<view class="all-reply-top">全部回复（{{ comment.replyList.length }}）</view>
			<view class="item" v-for="(item, index) in commentList" :key="item.id">
				<view class="comment">
					<view class="top">
						<view class="left">
							<view class="heart-photo"><image :src="item.fromHeadUrl" mode=""></image></view>
							<view class="user-info">
								<view class="name">{{ item.fromRealName }}</view>
								<view class="date">{{ $u.timeFrom(item.updateTime, 'yyyy-mm-dd') }}</view>
							</view>
						</view>
						<view class="bottom">
							<view class="reply" @click="commentShow(item)">回复</view>
						</view>
						
					</view>
					<view class="to">
						<text style="font-size: 30rpx; margin-right: 10rpx; margin-top: 5rpx;">回复</text>
						<view class="header"><image :src="item.toHeadUrl" mode=""></image></view>
						<view class="user_info">
							<view class="name">{{ item.toRealName }}</view>
						</view>
						<text style="font-size: 30rpx; margin-left: 10rpx; margin-top: 5rpx;">:</text>
					</view>
					<view class="reply">
						<view class="content">{{ item.content }}</view>
					</view>
					
				</view>
			</view>
		</view>
		<!-- 发送评论 -->
		<view class="plBox" v-if="comment_show">
			<textarea auto-height="true" 
					  maxlength="-1"
					  :show-confirm-bar="false"
					  cursor-spacing="100" 
					  fixed="true"
					  placeholder="评论" 
					  v-model="plValue"
					  @input="changeDisabled"
					  >
			</textarea>
			<button plain="true" @click="send">发送</button>
			<button plain="true" @click="commentFail" style="margin-left: 20rpx;">取消</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			commentList: [],
			comment: {},
			// 默认禁止点击按钮
			plValue: '',
			comment_show: false,
			comment_res: '',
			replyData: {},
		};
	},
	onLoad(params) {
		this.getReply(JSON.parse(decodeURIComponent(params.comment)));
	},
	methods: {
		// 回复列表
		getReply(comment) {
			this.comment = comment
			this.commentList = comment.replyList
		},
		//回复
		async reply(comment) {
			this.replyData.commentId = comment.commentId
			this.replyData.replyId = comment.replyId
			this.replyData.replyType = comment.replyType
			this.replyData.content = this.plValue
			this.replyData.fromUid = this.$store.state.vuex_user.openId
			this.replyData.toUid = comment.fromUid
			await this.$u.api.replyAdd(this.replyData).then((res) => {
				if(res.code == 200) {
					//成功后 置空、刷新评论
					// this.plValue = ''
					// this.getComment(this.helpInfo)
			 		uni.showToast({
			 			icon: "success",
			 			title: "回复成功"
			 		})
					//返回上一页
					const info = {
						id : comment.replyId,
					}
					let pages = getCurrentPages(); // 当前页面
					let beforePage = pages[pages.length - 2]; // 上一页
					uni.navigateBack({
					    success: function() {
							//执行onLoad 没生效，写了监听器
					        beforePage.onLoad({info: encodeURIComponent(JSON.stringify(info))}); // 执行上一页的onLoad方法
					    }
					});

			 	} else {
			 		uni.showToast({
			 			icon: "error",
			 			title: "回复失败"
			 		})
			 	}
			}) 
		}, 
		//评论输入框 动态监控
		changeDisabled(e) {
			console.log(e);
			this.plValue = e.detail.value
		},
		send() {
			if(this.plValue == '' || this.plValue == null) {
				uni.showToast({
					icon: "error",
					title: "内容不能为空"
				})
			} else {
				this.reply(this.comment_res)
			}
		},
		commentShow(res) {
			this.comment_show = true
			this.comment_res = res
		},
		commentFail() {
			this.comment_show = false
			this.plValue = ''
		},
	}
};
</script>

<style lang="scss" scoped>
page {
	background-color: #f2f2f2;
}
.comment {
	padding: 30rpx;
	font-size: 32rpx;
	background-color: #ffffff;
	.top {
		display: flex;
		justify-content: space-between;
	}
	.left {
		display: flex;
		.heart-photo {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}
		.user-info {
			margin-left: 10rpx;
			.name {
				color: #5677fc;
				font-size: 28rpx;
				margin-bottom: 4rpx;
			}
			.date {
				font-size: 20rpx;
				color: $u-light-color;
			}
		}
	}
	.bottom {
		margin-top: -10rpx;
		display: flex;
		font-size: 24rpx;
		color: #ffffff;
		.reply {
			color: #5677fc;
			margin-left: 10rpx;
		}
	}
	.right {
		display: flex;
		font-size: 20rpx;
		align-items: center;
		color: #9a9a9a;
		.like {
			margin-left: 6rpx;
		}
		.num{
			font-size: 26rpx;
			color: #9a9a9a;
		}
	}
	.highlight {
		color: #5677fc;
		.num{
			color: #5677fc;
		}
	}
}
.all-reply {
	margin-top: 10rpx;
	padding-top: 20rpx;
	background-color: #ffffff;
	.all-reply-top {
		margin-left: 20rpx;
		padding-left: 20rpx;
		border-left: solid 4rpx #5677fc;
		font-size: 30rpx;
		font-weight: bold;
	}
	.item {
		border-bottom: solid 2rpx $u-border-color;
	}
	.reply {
		padding: 20rpx;
		background-color: rgb(242, 242, 242);
		border-radius: 12rpx;
		margin: 10rpx 0;
	}
}
.to {
	display: flex;
	.header {
		image {
			width: 35rpx;
			height: 35rpx;
			border-radius: 50%;
			background-color: #f2f2f2;
			margin-top: 5rpx;
		}
	}
	.user_info {
		margin-left: 10rpx;
		.name {
			color: #5677fc;
			font-size: 28rpx;
			margin-top: 5rpx;
		}
		.date {
			font-size: 20rpx;
			color: $u-light-color;
		}
	}
}
	/* 发送评论 */
	.plBox {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		min-height: 100rpx;
		padding: 20rpx 20rpx 20rpx 30rpx;
		box-sizing: border-box;
		background-color: #F7F7F7;
		display: flex;
		justify-content: space-between;
		align-items: center;
	
		textarea {
			flex: 7;
			flex-shrink: 0;
			border-radius: 15rpx;
			max-height: 140rpx !important;
			margin-right: 30rpx;
			border: none;
			background-color: #ffffff;
			padding: 20rpx;
		}
	
		button {
			flex: 1;
			border: 1px solid #D3D3D3;
			color: #ffffff;
			margin: 0;
			padding: 0 5rpx;
			height: 60rpx;
			line-height: 60rpx;
			font-size: 30rpx;
			background-color: #304D99;
		}
	}

</style>
